﻿Public Class frmManageEmployeesBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    Dim empMgr As New EmployeeManager
    Private Sub frmManageEmployeesBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()

        Try
            Dim empList As List(Of Employee) = empMgr.getAllEmployees
            For Each emp As Employee In empList

                Dim item As ListViewItem
                item = lvwEmployee.Items.Add(emp.username)
                item.SubItems.Add(If((emp.role = "1"), "Admin", "Staff"))
                item.SubItems.Add(emp.salary)
                item.SubItems.Add(emp.time_in)
                item.SubItems.Add(emp.time_out)
                item.SubItems.Add(emp.id)

            Next
            grpEmployee.Text = "Total Employee(s): " + lvwEmployee.Items.Count.ToString()
            validator.resizes(lvwEmployee)
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Public Function ClearAllFields()
        validator.ResetControls(grpEmployee)
        Button1.Enabled = True
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        grpEmployee.Text = "Total Employee(s): 0"
        validator.resizes(lvwEmployee)
        Clipboard.Clear()
        Return 0
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        frmManageEmployeesDE.ShowDialog()
        frmManageEmployeesBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub lvwEmployee_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwEmployee.SelectedIndexChanged
        Button2.Enabled = (lvwEmployee.SelectedItems.Count = 1)
        Button3.Enabled = (lvwEmployee.SelectedItems.Count > 0)
        Button4.Enabled = (lvwEmployee.SelectedItems.Count = 1)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Clipboard.SetText(lvwEmployee.SelectedItems(0).SubItems(5).Text)
        frmManageEmployeesDE.ShowDialog()
        frmManageEmployeesBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If MessageBox.Show("Deleting this employee data will lose all information tagged in this employee", _
                "Do you want to Continue?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
        Else
            Try
                For i As Integer = 1 To lvwEmployee.SelectedItems.Count
                    'sqlManager.executeQuery("DELETE FROM tbl_sss WHERE id = " + lvwSSS.SelectedItems(i - 1).SubItems(7).Text)
                    empMgr.deleteItemEmployee(lvwEmployee.SelectedItems(i - 1).SubItems(5).Text)
                Next
                MsgBox("Record(s) has been deleted.")
                frmManageEmployeesBR_Load(vbNull, EventArgs.Empty)
                grpEmployee.Text = "Total Employee(s): " + lvwEmployee.Items.Count.ToString()
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If

        
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim employeeId As String = lvwEmployee.SelectedItems(0).SubItems(5).Text
        Clipboard.SetText(employeeId)
        frmEmployeeInfoBR.Show()

    End Sub
End Class